﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonLibrary.BLL;
using CommonLibrary.Model;

namespace SmsSeries
{
    public partial class frmUser : Form
    {
        #region 控件事件

        public frmUser()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 用户信息
        /// </summary>
        public UserInfoInfo _UserInfoInfo{get;set;}

        public bool isUpdate { get; set; }

        //确定
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if(isUpdate)
            {
                UserInfoInfo userInfoInfo = new UserInfoInfo(_UserInfoInfo.UserID,tbUserName.Text, tbPassWord.Text,
                                                             tbProvince.Text, tbCity.Text, Convert.ToDateTime(timePickerStart.Value),
                                                             timePickerEnd.Value, cbLocked.Checked,
                                                             tbDescription.Text);
                UserInfo.Update(userInfoInfo);

                Close();
            }
            else
            {
                List<UserInfoInfo> userInfoInfos = UserInfo.GetList("");
                UserInfoInfo _userInfoInfo = userInfoInfos.Where(o => o.UserName.Trim() == tbUserName.Text.Trim()).FirstOrDefault();

                if (_userInfoInfo == null)
                {
                    UserInfoInfo userInfoInfo = new UserInfoInfo(tbUserName.Text, tbPassWord.Text,
                                                             tbProvince.Text, tbCity.Text, timePickerStart.Value,
                                                             timePickerEnd.Value, cbLocked.Checked,
                                                             tbDescription.Text);

                    UserInfo.Insert(userInfoInfo);

                    Close();
                }
                else
                {
                    MessageBox.Show("用户名已存在！");
                }
            }
        }

        #endregion

        private void frmUser_Load(object sender, EventArgs e)
        {
            timePickerStart.Value = DateTime.Now.Date;

            timePickerEnd.Value = DateTime.Now.Date;
            timePickerEnd.Value = DateTime.Now.Date.AddHours(23).AddMinutes(59).AddSeconds(59);

            if(isUpdate && _UserInfoInfo !=null)
            {
                this.Text = "编辑用户";
                tbUserName.Text = _UserInfoInfo.UserName;
                tbPassWord.Text = _UserInfoInfo.Password;
                tbProvince.Text = _UserInfoInfo.ProvinceID;
                tbCity.Text = _UserInfoInfo.CityID;
                timePickerStart.Text = _UserInfoInfo.StartTime.ToString();
                timePickerEnd.Text = _UserInfoInfo.EndTime.ToString();
                cbLocked.Checked = _UserInfoInfo.Locked;
                tbDescription.Text = _UserInfoInfo.Description;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
